      SUBROUTINE CBALEV5  
C  
C CHANGE RECORD  
C **  SUBROUTINES CBALEV CALCULATE GLOBAL VOLUME, MASS, MOMENTUM,  
C **  AND ENERGY BALANCES  
C  
      USE GLOBAL  
C  
C **  CHECK FOR END OF BALANCE PERIOD  
C  
      NTMPD2=NTSMMT/2  
      IF(NBALE.EQ.NTMPD2)THEN  
C  
C **  CALCULATE ENDING VOLUME, SALT MASS, DYE MASS, MOMENTUM, KINETIC  
C **  ENERGY AND POTENTIAL ENERGY, AND ASSOCIATED FLUXES  
C  
        VOLENDE=0.  
        SALENDE=0.  
        DYEENDE=0.  
        UMOENDE=0.  
        VMOENDE=0.  
        UUEENDE=0.  
        VVEENDE=0.  
        PPEENDE=0.  
        BBEENDE=0.  
        DO L=2,LA  
          LN=LNC(L)  
          VOLENDE=VOLENDE+SPB(L)*DXYP(L)*HP(L)  
          UMOENDE=UMOENDE+SPB(L)*0.5*DXYP(L)*HP(L)*(DYIU(L)*HUI(L)
     &*UHDYE(L)                +DYIU(LEAST(L))*HUI(LEAST(L))*UHDYE(LEAST(L)))  
          VMOENDE=VMOENDE+SPB(L)*0.5*DXYP(L)*HP(L)*(DXIV(L)*HVI(L)
     &*VHDXE(L)                +DXIV(LN)*HVI(LN)*VHDXE(LN))  
          PPEENDE=PPEENDE+SPB(L)*0.5*DXYP(L)  
     &        *(GI*P(L)*P(L)-G*BELV(L)*BELV(L))  
        ENDDO  
        AMOENDE=SQRT(UMOENDE*UMOENDE+VMOENDE*VMOENDE)  
        DO K=1,KC  
          DO L=2,LA  
            LN=LNC(L)  
            SALENDE=SALENDE+SCB(L)*DXYP(L)*HP(L)*SAL(L,K)*DZC(K)  
            DYEENDE=DYEENDE+SCB(L)*DXYP(L)*HP(L)*DYE(L,K)*DZC(K)  
            UUEENDE=UUEENDE+SPB(L)*0.125*DXYP(L)*HP(L)*DZC(K)  
     &          *( (U(L,K)+U(LEAST(L),K))*(U(L,K)+U(LEAST(L),K)) )  
            VVEENDE=VVEENDE+SPB(L)*0.125*DXYP(L)*HP(L)*DZC(K)  
     &          *( (V(L,K)+V(LN,K))*(V(L,K)+V(LN,K)) )  
            BBEENDE=BBEENDE+SPB(L)*GP*DXYP(L)*HP(L)*DZC(K)*( BELV(L)  
     &          +0.5*HP(L)*(Z(K)+Z(K-1)) )*B(L,K)  
          ENDDO  
        ENDDO  
        UUEOUTE=DT2*UUEOUTE  
        VVEOUTE=DT2*VVEOUTE  
        PPEOUTE=DT2*PPEOUTE  
        BBEOUTE=DT2*BBEOUTE  
        VOLOUTE=DT2*VOLOUTE  
        SALOUTE=DT2*SALOUTE  
        DYEOUTE=DT2*DYEOUTE  
        UMOOUTE=DT2*UMOOUTE  
        VMOOUTE=DT2*VMOOUTE  
        ENEBEGE=UUEBEGE+VVEBEGE+PPEBEGE+BBEBEGE  
        ENEENDE=UUEENDE+VVEENDE+PPEENDE+BBEENDE  
        ENEOUTE=UUEOUTE+VVEOUTE+PPEOUTE+BBEOUTE  
        VOLBMOE=VOLBEGE-VOLOUTE  
        SALBMOE=SALBEGE-SALOUTE  
        DYEBMOE=DYEBEGE-DYEOUTE  
        UMOBMOE=UMOBEGE-DYEOUTE  
        VMOBMOE=VMOBEGE-DYEOUTE  
        ENEBMOE=ENEBEGE-ENEOUTE  
        VOLERR=VOLENDE-VOLBMOE  
        SALERR=SALENDE-SALBMOE  
        DYEERR=DYEENDE-DYEBMOE  
        UMOERR=UMOENDE-UMOBMOE  
        VMOERR=VMOENDE-VMOBMOE  
        ENEERR=ENEENDE-ENEBMOE  
        RVERDE=-9999.  
        RSERDE=-9999.  
        RDERDE=-9999.  
        RUERDE=-9999.  
        RVERDE=-9999.  
        REERDE=-9999.  
        RVERDO=-9999.  
        RSERDO=-9999.  
        RDERDO=-9999.  
        RUERDO=-9999.  
        RVERDO=-9999.  
        REERDO=-9999.  
        IF(VOLENDE.NE.0.) RVERDE=VOLERR/VOLENDE  
        IF(SALENDE.NE.0.) RSERDE=SALERR/SALENDE  
        IF(DYEENDE.NE.0.) RDERDE=DYEERR/DYEENDE  
        IF(UMOENDE.NE.0.) RUMERDE=UMOERR/UMOENDE  
        IF(VMOENDE.NE.0.) RVMERDE=VMOERR/VMOENDE  
        IF(ENEENDE.NE.0.) REERDE=ENEERR/ENEENDE  
        IF(VOLOUTE.NE.0.) RVERDO=VOLERR/VOLOUTE  
        IF(SALOUTE.NE.0.) RSERDO=SALERR/SALOUTE  
        IF(DYEOUTE.NE.0.) RDERDO=DYEERR/DYEOUTE  
        IF(UMOOUTE.NE.0.) RUMERDO=UMOERR/UMOOUTE  
        IF(VMOOUTE.NE.0.) RVMERDO=VMOERR/VMOOUTE  
        IF(ENEOUTE.NE.0.) REERDO=ENEERR/ENEOUTE  
C  
C **  OUTPUT BALANCE RESULTS TO FILE BALE.OUT  
C  
        IF(JSBALE.EQ.1)THEN  
          OPEN(89,FILE='BALE.OUT',STATUS='UNKNOWN')  
          CLOSE(89,STATUS='DELETE')  
          OPEN(89,FILE='BALE.OUT',STATUS='UNKNOWN')  
          JSBALE=0  
        ELSE  
          OPEN(89,FILE='BALE.OUT',POSITION='APPEND',STATUS='UNKNOWN')  
        ENDIF  
        WRITE(89,890)NTMPD2,N  
        WRITE(89,891)  
        WRITE(89,892)VOLBEGE,SALBEGE,DYEBEGE,ENEBEGE,UMOBEGE,VMOBEGE,  
     &      AMOBEGE  
        WRITE(89,900)  
        WRITE(89,893)  
        WRITE(89,892)VOLOUTE,SALOUTE,DYEOUTE,ENEOUTE,UMOOUTE,VMOOUTE  
        WRITE(89,900)  
        WRITE(89,894)  
        WRITE(89,892)VOLBMOE,SALBMOE,DYEBMOE,ENEBMOE,UMOBMOE,VMOBMOE  
        WRITE(89,900)  
        WRITE(89,895)  
        WRITE(89,892)VOLENDE,SALENDE,DYEENDE,ENEENDE,UMOENDE,VMOENDE,  
     &      AMOENDE  
        WRITE(89,900)  
        WRITE(89,896)  
        WRITE(89,892)VOLERR,SALERR,DYEERR,ENEERR,UMOERR,VMOERR  
        WRITE(89,900)  
        WRITE(89,897)  
        WRITE(89,892)RVERDE,RSERDE,RDERDE,REERDE,RUMERDE,RVMERDE  
        WRITE(89,900)  
        WRITE(89,898)  
        WRITE(89,892)RVERDO,RSERDO,RDERDO,REERDO,RUMERDO,RVMERDO  
        WRITE(89,899)  
        UUEBMOE=UUEBEGE-UUEOUTE  
        VVEBMOE=VVEBEGE-VVEOUTE  
        PPEBMOE=PPEBEGE-PPEOUTE  
        BBEBMOE=BBEBEGE-BBEOUTE  
        WRITE(89,901)UUEBEGE  
        WRITE(89,902)UUEOUTE  
        WRITE(89,903)UUEBMOE  
        WRITE(89,904)UUEENDE  
        WRITE(89,900)  
        WRITE(89,905)VVEBEGE  
        WRITE(89,906)VVEOUTE  
        WRITE(89,907)VVEBMOE  
        WRITE(89,908)VVEENDE  
        WRITE(89,900)  
        WRITE(89,909)PPEBEGE  
        WRITE(89,910)PPEOUTE  
        WRITE(89,911)PPEBMOE  
        WRITE(89,912)PPEENDE  
        WRITE(89,900)  
        WRITE(89,913)BBEBEGE  
        WRITE(89,914)BBEOUTE  
        WRITE(89,915)BBEBMOE  
        WRITE(89,916)BBEENDE  
        WRITE(89,900)  
        WRITE(89,899)  
        CLOSE(89)  
  890 FORMAT (' VOLUME, MASS, AND ENERGY BALANCE OVER',I5,' TIME STEPS'  
     &    ,' ENDING AT TIME STEP',I5,//)  
  891 FORMAT (' INITIAL VOLUME    INITIAL SALT    INITIAL DYE     '  
     &    ,'INITIAL ENER    INITIAL UMO     INITIAL VMO     '  
     &    ,'INITIAL AMO',/)  
  892 FORMAT (1X,7(E14.6,2X))  
  893 FORMAT (' VOLUME OUT        SALT OUT        DYE OUT         '  
     &    ,'ENERGY OUT      UMO OUT         VMO OUT',/)  
  894 FORMAT (' INITIAL-OUT VOL   INIT-OUT SALT   INIT-OUT DYE    '  
     &    ,'INIT-OUT ENER   INIT-OUT UMO    INIT-OUT VMO',/)  
  895 FORMAT (' FINAL VOLUME      FINAL SALT      FINAL DYE       '  
     &    ,'FINAL ENERGY    FINAL UMO       FINAL VMO       '  
     &    ,'FINAL AMO',/)  
  896 FORMAT (' VOLUME ERR        SALT ERR        DYE ERR         '  
     &    ,'ENERGY ERR      UMO ERR         VMO ERR',/)  
  897 FORMAT (' R VOL/END ER      R SAL/END ER    R DYE/END ER    '  
     &    ,'R ENE/END ER    R UMO/END ER    R VMO/END ER',/)  
  898 FORMAT (' R VOL/OUT ER      R SAL/OUT ER    R DYE/OUT ER    '  
     &    ,'R ENE/OUT ER    R UMO/OUT ER    R VMO/OUT ER',/)  
  899 FORMAT (////)  
  900 FORMAT (//)  
  901 FORMAT(' UUEBEGE =  ',E14.6)  
  902 FORMAT(' UUEOUTE =  ',E14.6)  
  903 FORMAT(' UUEBMOE =  ',E14.6)  
  904 FORMAT(' UUEENDE =  ',E14.6)  
  905 FORMAT(' VVEBEGE =  ',E14.6)  
  906 FORMAT(' VVEOUTE =  ',E14.6)  
  907 FORMAT(' VVEBMOE =  ',E14.6)  
  908 FORMAT(' VVEENDE =  ',E14.6)  
  909 FORMAT(' PPEBEGE =  ',E14.6)  
  910 FORMAT(' PPEOUTE =  ',E14.6)  
  911 FORMAT(' PPEBMOE =  ',E14.6)  
  912 FORMAT(' PPEENDE =  ',E14.6)  
  913 FORMAT(' BBEBEGE =  ',E14.6)  
  914 FORMAT(' BBEOUTE =  ',E14.6)  
  915 FORMAT(' BBEBMOE =  ',E14.6)  
  916 FORMAT(' BBEENDE =  ',E14.6)  
        NBALE=0  
      ENDIF  
      NBALE=NBALE+1  
      RETURN  
      END  

